<?php
// 应用公共文件

if (!function_exists('curl')) {
    /**
     * 网络请求
     * @param string $url
     * @param array $params
     * @param string $type
     * @return array
     */
    function curl(string $url = '', array $params = [], string $type = 'get'): array
    {
        $ch = curl_init();
        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        } else if (!empty($params)) {
            $params = http_build_query($params);
            $url .= '?' . $params;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return [false, curl_error($ch)];
        }
        curl_close($ch);

        return [true, $result];
    }
}

if (!function_exists('jsonCurl')) {
    /**
     * 网络请求 post json数据格式
     * @param string $url
     * @param array $params
     * @param string $type
     * @return array
     */
    function jsonCurl(string $url = '', array $params = []): array
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return [false, curl_error($ch)];
        }
        curl_close($ch);

        return [true, $result];
    }
}

if (!function_exists('getHeaderToken')) {
    function getHeaderToken($header): string
    {
        $authorization = $header['authorization'] ?? '';
        return substr($authorization, 7); //Bearer Hb1wJLLUQgkf-jQia-wH_2f6Fpo-z2l5
    }
}

if (!function_exists('getHeaderDeviceType')) {
    function getHeaderDeviceType($header): string
    {
        return $header['device-type'] ?? '';
    }
}

if (!function_exists('getHeaderAppKey')) {
    function getHeaderAppKey($header): string
    {
        return $header['app-key'] ?? '';
    }
}



if (!function_exists('pagination')) {
    /**
     * 返回自定义分页参数结构
     * @param \think\Paginator $pagination
     * @return array
     */
    function pagination(\think\Paginator $pagination)
    {
        return [
            'items' => $pagination->items(),
            'pageInfo' => [
                'currentPage' => $pagination->currentPage(),
                'total' => $pagination->total(),
                'totalPage' => $pagination->lastPage()
            ]
        ];
    }
}



